package dao.Impl;

import dao.ICompanyDao;
import dto.CompanyModelDto;
import model.CompanyModel;
import util.GetSqlSession;
import util.JDBC;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CompanyImpl implements ICompanyDao {


    @Override
    public CompanyModelDto getCommonModelDto() {
      CompanyModelDto companyModelDto = GetSqlSession.getSqlSession().selectOne("dao.ICompanyDao.getCommonModelDto");
      GetSqlSession.commit();
        return companyModelDto;
    }

    @Override
    public int add(CompanyModel companyModel) {
       int a =GetSqlSession.getSqlSession().insert("dao.ICompanyDao.add",companyModel);
       GetSqlSession.commit();
        return a;
    }

    @Override
    public int update(CompanyModel companyModel) {
//        String sql = "UPDATE  from * `hs_company` SET " +
//                "`logo` = '" + companyModel.getLogo() + "', " +
//                "`phone` = '" + companyModel.getPhone() + "'," +
//                " `ad` = '" + companyModel.getAd() + "', " +
//                "`html` = '" + companyModel.getHtml() + "', " +
//                " `email` = '" + companyModel.getEmail() + "'," +
//                " `linePhone` = '" + companyModel.getLinePhone() + "'," +
//                " `address` = '" + companyModel.getAddress() + "', " +
//                " `edition` = '" + companyModel.getEdition() + "' where id=" + companyModel.getId();
        int b =GetSqlSession.getSqlSession().update("dao.ICompanyDao.update",companyModel);
        GetSqlSession.commit();
        return b;
    }

    @Override
    public int del(CompanyModel companyModel) {
//        String sql = "delete from `hs_company` where id=" + companyModel.getId();
        int c =GetSqlSession.getSqlSession().delete("dao.ICompanyDao.del",companyModel);
        GetSqlSession.commit();
        return c;
    }

    @Override
    public List<CompanyModel> findAll() {

        return GetSqlSession.getSqlSession().selectList("dao.ICompanyDao.findAll");

    }

    @Override
    public CompanyModel findById(CompanyModel companyModel) {
        return GetSqlSession.getSqlSession().selectOne("dao.ICompanyDao.findById");
    }

    /**
     * like '%%'  模糊查询
     *
     * @param companyModel
     * @return
     */
    @Override
    public List<CompanyModel> findBySome(CompanyModel companyModel) {
//        String sql = "SELECT * FROM `hs_company` where 1=1";
//        if (!companyModel.getPhone().equals("")) {
//            sql += " and phone like '%" + companyModel.getPhone() + "%'";
//        }
//        if (!companyModel.getEmail().equals("")) {
//            sql += " and email like '%" + companyModel.getEmail() + "%'";
//        }
//        if (!companyModel.getHtml().equals("")) {
//            sql += " and html like '%" + companyModel.getHtml() + "%'";
//        }
//        if (!companyModel.getAddress().equals("")) {
//            sql += " and address like '%" + companyModel.getAddress() + "%'";
//        }
//        ResultSet resultSet = JDBC.getResultSet(sql);
//        List<CompanyModel> list = new ArrayList<>();
//
//        try {
//            while (resultSet.next()) {
//                CompanyModel model = new CompanyModel();
//                model.setId(resultSet.getInt("id"));
//                model.setLogo(resultSet.getString("logo"));
//                model.setPhone(resultSet.getString("phone"));
//                model.setAd(resultSet.getString("ad"));
//                model.setHtml(resultSet.getString("html"));
//                model.setEmail(resultSet.getString("email"));
//                model.setLinePhone(resultSet.getString("linePhone"));
//                model.setAddress(resultSet.getString("address"));
//                model.setEdition(resultSet.getString("edition"));
//                list.add(model);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            JDBC.close();
//        }
        return GetSqlSession.getSqlSession().selectOne("dao.ICompanyDao.findBySome",companyModel);
    }
}

